Return a dynamic string from std::exception's `what`
Posted
by Shtééf
on Stack Overflow
See other posts from Stack Overflow
or by Shtééf
Published on 2010-04-10T16:43:39Z
Indexed on
2010/04/10
16:53 UTC
Read the original article
Hit count: 343
I'm convinced at this point that I should be creating subclasses of std::exception
for all my exception throwing needs. Now I'm looking at how to override the what
method.
The situation that I'm facing, it would be really handy if the string what
returns be dynamic. Some pieces of code parse an XML file for example, and adding a position or line number to the error message is useful to me.
What I'd like to know:
what
returns aconst char *
, which implies any catcher is likely not going to free the string. So I need some other place to store the result, but where would that be? (I need thread-safety.)what
also includesthrow()
in its signature. While I can prevent mywhat
from throwing anything, it seems to me that this method really isn't intended for anything too dynamic. Ifwhat
is not the right place, then where should I be doing this instead?
© Stack Overflow or respective owner